﻿using System;
using System.Transactions;
using Common;
using Dto;
using SqlServerDAL;
using System.Collections.Generic;

namespace Business
{
    public class ActivityBusiness
    {
        private const string Waming = "Business.ActivityBusiness";
        private readonly GroupActivityDAL _gaDal = new GroupActivityDAL();
        private readonly ActivityPartnerDAL _apDal = new ActivityPartnerDAL();

        public bool SaveGroupActivity(GroupActivityDto dto)
        {
            try
            {
                var b = _gaDal.Save(dto);
                return b > 0;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".SaveGroupActivity(GroupActivityDto dto)", ex);
                return false;
            }
        }

        public bool CreateGroupActivity(GroupActivityDto dto, List<ActivityPartnerDto> activityPartnerDtos)
        {
            var tran = new TransactionScope();
            try
            {
                var b = true;
                var gaId = _gaDal.Save(dto);
                if (gaId > 0)
                {
                    foreach (var activityPartnerDto in activityPartnerDtos)
                    {
                        if (!b) continue;
                        activityPartnerDto.ApActivityId = gaId;
                        b = _apDal.Save(activityPartnerDto);
                    }
                }
                else
                {
                    b = false;
                }
                if (b)
                {
                    tran.Complete();
                }
                return b;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".CreateGroupActivity(GroupActivityDto dto, List<ActivityPartnerDto> activityPartnerDtos)", ex);
                return false;
            }
            finally
            {
                tran.Dispose();
            }
        }

        public List<GroupActivityDto> GetGroupActivityListByChannelId(int pageNo, int pageSize, int channelId, out int totalCount)
        {
            try
            {
                List<GroupActivityDto> dtos = null;
                var hts = _gaDal.SlicedFindAll((pageNo - 1) * pageSize + 1, pageNo * pageSize, 0, channelId, "");
                if (hts != null)
                {
                    dtos = new List<GroupActivityDto>();
                    foreach (var ht in hts)
                    {
                        var dto = FillDto.FillGroupActivityDto(ht);
                        dtos.Add(dto);
                    }
                }
                totalCount = _gaDal.SelectCount(0, channelId);
                return dtos;
            }
            catch (Exception ex)
            {
                totalCount = -1;
                Log.LogHandler.LogError(Waming + ".GetGroupActivityList(int pageNo, int pageSize, int groupId, out int totalCount)", ex);
                return null;
            }
        }
    }
}
